//
//  MPFilePath.m
//  MiaoPai
//
//  Created by Jeakin on 14/6/17.
//  Copyright (c) 2014年 Xuan Yi Xia Technology Co., LTD. All rights reserved.
//

#import "MPTFilePath.h"

@interface MPTFilePath ()
@property (nonatomic, readwrite, assign) BOOL		isJailbroken;
@property (nonatomic, readwrite, strong)   NSString	*homePath;
@property (nonatomic, readwrite, strong)   NSString	*docmentsPath;
@property (nonatomic, readwrite, strong)   NSString	*libraryPath;
@property (nonatomic, readwrite, strong)   NSString	*dbPath;
@property (nonatomic, readwrite, strong)   NSString	*tmpPath;
@property (nonatomic, readwrite, strong)   NSString	*cachesPath;
@property (nonatomic, readwrite, strong)   NSString	*preferencesPath;
@property (nonatomic, readwrite, strong)   NSString	*bundlePath;
@property (nonatomic, readwrite, strong)   NSString	*downloadPath;
@property (nonatomic, readwrite, strong)   NSString	*coredataPath;
@property (nonatomic, readwrite, strong)   NSString	*mediaPath;
@property (nonatomic, readwrite, strong)   NSString	*themePath;
@property (nonatomic, readwrite, strong)   NSString	*musicPath;
@property (nonatomic, readwrite, strong)   NSString	*videoPath;
@property (nonatomic, readwrite, strong)   NSString	*photoPath;
@property (nonatomic, readwrite, strong)   NSString *pasterPath;
@property (nonatomic, readwrite, strong)   NSString *operaPath;
@property (nonatomic, readwrite, strong)   NSString *monkeyPath;
@property (nonatomic, readwrite, strong)   NSString  *faceStickerPath;
@property (nonatomic, readwrite, strong)   NSString	*tmpVideoPath;
@property (nonatomic, readwrite, strong)   NSString	*tmpVideoInfoPath;
@property (nonatomic, readwrite, strong)   NSString	*qiniuRecorderPath;
@property (nonatomic, readwrite, strong)   NSString	*inboxPath;
@property (nonatomic, readwrite, strong)   NSString	*historyDiscoversPath;
@property (nonatomic, readwrite, strong)   NSString	*facePath;
// Documents／insDraft
@property (nonatomic, readwrite, strong) NSString *insDraftPath;


@end

@implementation MPTFilePath

- (void)initialize
{
    [self initDirPath];
}

- (void)initDirPath
{
    self.homePath = NSHomeDirectory();
    self.docmentsPath = [self.homePath stringByAppendingPathComponent:@"Documents"];
    self.libraryPath = [self.homePath stringByAppendingPathComponent:@"Library"];
    self.tmpPath = [self.homePath stringByAppendingPathComponent:@"tmp"];
    self.cachesPath = [self.homePath stringByAppendingPathComponent:@"Library/Caches"];
    self.preferencesPath = [self.homePath stringByAppendingPathComponent:@"Library/Preferences"];
    self.bundlePath = [[NSBundle mainBundle] bundlePath];
    self.downloadPath = [self.homePath stringByAppendingPathComponent:@"Library/Caches/Download"];
    self.coredataPath = [self.homePath stringByAppendingPathComponent:@"Library/Caches/CoreData"];
    self.mediaPath = [self.docmentsPath stringByAppendingPathComponent:@"media"];
    /// mv 和 滤镜
    self.themePath = [self.docmentsPath stringByAppendingPathComponent:@"MVAndLJ"];
    /// 改个名称 为了不兼容老版本
    self.musicPath = [self.docmentsPath stringByAppendingPathComponent:@"musicYinYue"];
    self.videoPath = [self.docmentsPath stringByAppendingPathComponent:@"video"];
    self.photoPath = [self.docmentsPath stringByAppendingPathComponent:@"photo"];
    self.pasterPath = [self.docmentsPath stringByAppendingPathComponent:@"paster"];
    self.operaPath = [self.docmentsPath stringByAppendingPathComponent:@"opera"];
    self.monkeyPath = [self.docmentsPath stringByAppendingPathComponent:@"monkey"];
    self.faceStickerPath = [self.docmentsPath stringByAppendingPathComponent:@"faceSticker"];
    self.tmpVideoPath = [self.docmentsPath stringByAppendingPathComponent:@"tmpVideo"];
    self.dbPath  = [self.docmentsPath stringByAppendingPathComponent:@"db.sqlite3"];
    self.tmpVideoInfoPath = [self.docmentsPath stringByAppendingPathComponent:@"temp.plist"];
    self.qiniuRecorderPath = [self.docmentsPath stringByAppendingPathComponent:@"stream"];
    self.inboxPath = [self.docmentsPath stringByAppendingPathComponent:@"Inbox"];
    self.historyDiscoversPath = [self.docmentsPath stringByAppendingPathComponent:@"historyDiscovers.plist"];
    self.facePath = [self.docmentsPath stringByAppendingPathComponent:@"face"];
    self.insDraftPath = [self.docmentsPath stringByAppendingPathComponent:@"insDraft"];

    [self createDirPathIfNeed:self.homePath];
    [self createDirPathIfNeed:self.docmentsPath];
    
    [self createDirPathIfNeed:self.mediaPath];
    [self createDirPathIfNeed:self.themePath];
    [self createDirPathIfNeed:self.musicPath];
    [self createDirPathIfNeed:self.videoPath];
    [self createDirPathIfNeed:self.photoPath];
    [self createDirPathIfNeed:self.tmpVideoPath];
    [self createDirPathIfNeed:self.qiniuRecorderPath];
    [self createDirPathIfNeed:self.facePath];
    [self createDirPathIfNeed:self.insDraftPath];
    [self addSkipBackupAttributeToItemAtPath:self.docmentsPath];
}

- (NSString *)createDirPathIfNeed:(NSString*)dirPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ( ![fileManager fileExistsAtPath:dirPath] ) {
        [fileManager createDirectoryAtPath:dirPath
               withIntermediateDirectories:YES
                                attributes:nil
                                     error:NULL];
    }
    return dirPath;
}

- (id)init {
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

+ (MPTFilePath *)sharedInstance {
    static MPTFilePath *mpFilePath = nil;
    if (!mpFilePath) {
        @synchronized(self) {
            if (!mpFilePath) mpFilePath = [[self alloc] init];
        }
    }
    return mpFilePath;
}


- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
{
    NSURL* URL= [NSURL fileURLWithPath: filePathString];
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

- (void)clearInboxFilesIfNeed
{
    dispatch_queue_t dispatchQueue = dispatch_queue_create("com.miaopai.inbox.clear.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t dispatchGroup = dispatch_group_create();
    dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
        NSError *error = nil;
        NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.inboxPath error:&error];
        
        if(error == nil)
        {
            NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];;
            for(NSString *path in files)
            {
                NSString *itemPath = [self.inboxPath stringByAppendingPathComponent:path];
                NSDictionary *properties = [[NSFileManager defaultManager] attributesOfItemAtPath:itemPath error:&error];
                
                if(error == nil)
                {
                    NSDate *date = [properties objectForKey:NSFileCreationDate];
                    NSComparisonResult result = [yesterday compare:date];
                    if (result == NSOrderedDescending) {
                        [[NSFileManager defaultManager] removeItemAtPath:itemPath error:nil];
                    }
                }
            }
        }
    });
}

@end
